package com.robot.base.view

import android.content.Context
import android.content.res.AssetFileDescriptor
import android.media.MediaPlayer
import android.util.AttributeSet
import android.view.SurfaceHolder
import android.view.SurfaceView
import com.blankj.utilcode.util.LogUtils
import java.io.IOException
import java.text.SimpleDateFormat

class MySurfaceView : SurfaceView, SurfaceHolder.Callback {
    private var surfaceHolder: SurfaceHolder? = null
    private var mediaPlayer: MediaPlayer? = null

    constructor(context: Context?) : super(context) {

    }

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {

    }

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
    }

    constructor(
        context: Context?,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int
    ) : super(context, attrs, defStyleAttr, defStyleRes) {
    }

    init {
        surfaceHolder = holder
        surfaceHolder?.addCallback(this)
        if (mediaPlayer == null) {
            mediaPlayer = MediaPlayer()
        }
        LogUtils.e("init")
    }

    fun setDataPath(path: String?) {
        mediaPlayer?.reset()
        try {
            mediaPlayer?.setDataSource(path)
            mediaPlayer?.prepareAsync()
            mediaPlayer?.setOnPreparedListener { mediaPlayer!!.start() }
        } catch (e: IOException) {
            e.printStackTrace()
        }
    }

    fun setDataPath(assetFileDescriptor: AssetFileDescriptor,
                    resultFunc: () -> Unit) {
        LogUtils.e("setDataPath")
        mediaPlayer?.reset()
        try {
            mediaPlayer?.setDataSource(
                assetFileDescriptor.fileDescriptor,
                assetFileDescriptor.startOffset, assetFileDescriptor.length
            )
            mediaPlayer?.isLooping = true
            mediaPlayer?.prepareAsync()
            mediaPlayer?.setOnPreparedListener {
                LogUtils.e("start")
                resultFunc.invoke()
                it.start()
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    var bind = false
    override fun surfaceCreated(holder: SurfaceHolder) {
        LogUtils.e("surfaceCreated")
        mediaPlayer?.setDisplay(surfaceHolder)
    }

    override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {}

    override fun surfaceDestroyed(holder: SurfaceHolder) {
        LogUtils.e("surfaceDestroyed")
        bind = true
//        if (mediaPlayer == null) {
//            return
//        }
//        if (mediaPlayer?.isPlaying==true) {
//            LogUtils.e("mediaPlayer?.pause()")
//            mediaPlayer?.pause()
//        }
//        if (mediaPlayer != null) {
//            mediaPlayer?.release()
//            mediaPlayer = null
//        }
    }

    //开始播放
    fun play() {
        LogUtils.e("play")
        if (mediaPlayer != null) {
            if (mediaPlayer?.isPlaying != true) {
                LogUtils.e("mediaPlayer?.isPlaying")
                mediaPlayer?.start()
            }
        }
    }

    //暂停
    fun pause() {
        if (mediaPlayer != null) {
            if (mediaPlayer?.isPlaying == true) {
                LogUtils.e("mediaPlayer?.pause()")
                mediaPlayer?.pause()
            }
        }
    }

    //销毁
    fun destory() {
        LogUtils.e("destory")
        if (mediaPlayer != null) {
            if (mediaPlayer?.isPlaying == true) {
                mediaPlayer?.stop()
            }
            mediaPlayer?.release()
            mediaPlayer = null
        }
    }

    //拖动更新进度
    fun seekTo(progress: Int) {
        if (mediaPlayer != null) {
            val duration = mediaPlayer?.duration ?: 0
            val current = progress * duration / 100
            mediaPlayer?.seekTo(current)
        }
    }

    //获取播放进度
    val progress: Int
        get() {
            if (mediaPlayer != null) {
                val druation = mediaPlayer?.duration ?: 1
                val currentPosition = mediaPlayer?.currentPosition ?: 0
                return currentPosition * 100 / druation
            }
            return 0
        }

    //获取播放时长
    val currentTime: String
        get() {
            if (mediaPlayer != null) {
                val currentPostion = mediaPlayer?.currentPosition ?: 0
                val format = SimpleDateFormat("mm:ss")
                val f: String = format.format(currentPostion)
                return f + ""
            }
            return ""
        }

    //获取时长
    val duration: String
        get() {
            if (mediaPlayer != null) {
                val duration = mediaPlayer?.duration ?: 0
                val format = SimpleDateFormat("mm:ss")
                return format.format(duration).toString() + ""
            }
            return ""
        }
}